package com.myProject.control.expert;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TArea;
import com.myProject.bean.TClass;
import com.myProject.bean.TExpert;
import com.myProject.serves.ExpertSev;
import com.myProject.serves.Impl.ExpertSevImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ExpertServlet", urlPatterns = "/expert")
public class ExpertServlet extends HttpServlet {
    ExpertSev es = new ExpertSevImpl();

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:21
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:21
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            showAll(request, response);
        } else if ("showAllExpert".equalsIgnoreCase(med)) {
            showAllExpert(request, response);
        } else if ("alt".equalsIgnoreCase(med)) {
            updateExpert(request, response);
        } else if ("delete".equalsIgnoreCase(med)) {
            deleteExpert(request, response);
        } else if ("add".equalsIgnoreCase(med)) {
            addExpert(request, response);
        }
    }

    private void addExpert(HttpServletRequest request, HttpServletResponse response) {
        String expertName = request.getParameter("expertName");
        String company = request.getParameter("company");
        String strong = request.getParameter("strong");
        String job = request.getParameter("job");
        long tel = Long.parseLong(request.getParameter("tel"));
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        TExpert t = new TExpert();
        t.setCompany(company);
        t.setName(expertName);
        t.setCompany(company);
        t.setStrong(strong);
        t.setJob(job);
        t.setTel(tel);
        t.setSex(sex);
        t.setBirthday(birthday);
        t.setEmail(email);
        t.setAddress(address);
        System.out.println("t = " + t);
        boolean flag = es.addExpert(t);
    }

    private void deleteExpert(HttpServletRequest request, HttpServletResponse response) {
        String x = request.getParameter("pkId");
        int pkId = Integer.parseInt(x);
        TExpert te = new TExpert();
        te.setPkId(pkId);
        boolean flag = es.deleteExpert(te);
        System.out.println("flag = " + flag);
    }

    private void updateExpert(HttpServletRequest request, HttpServletResponse response) {
        int pkId = Integer.parseInt(request.getParameter("pkId"));
        String email = request.getParameter("email");
        String tel = request.getParameter("tel");
        long tels = Long.parseLong(tel);
        String job = request.getParameter("job");
        String company = request.getParameter("company");

        TExpert tExpert = new TExpert();
        tExpert.setPkId(pkId);
        tExpert.setEmail(email);
        tExpert.setTel(tels);
        tExpert.setJob(job);
        tExpert.setCompany(company);
        es.updateExpert(tExpert);
    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);
        String expertName = request.getParameter("expertName");
        String strong = request.getParameter("strong");
        String company = request.getParameter("company");
        TExpert tExpert = new TExpert();
        tExpert.setName(expertName);
        tExpert.setStrong(strong);
        tExpert.setCompany(company);
        PageInfo<TExpert> show = es.showExpertPage(pageNo, pageSize, tExpert);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);
    }

    private void showAllExpert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        List<TExpert> list = es.getAllExpert();
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        System.out.println("json = " + json);
        pw.write(json);
    }


}
